package svc

import (
	"go-weixin/app/guitar/rpc/internal/config"
	"go-weixin/common/chendb"
	"go-weixin/common/chendb/xorms"
	"go-weixin/common/es"
	"go-weixin/common/rpcserver"

	"github.com/zeromicro/go-queue/kq"

	"github.com/zeromicro/go-zero/core/stores/redis"
)

type ServiceContext struct {
	Config    config.Config
	ChenDb    *chendb.ChenDb
	ChenCache *redis.Redis
	RpcMDLE   *rpcserver.Middleware
	KqClient  *kq.Pusher
	Es        *es.Es
}

func NewServiceContext(c config.Config) *ServiceContext {
	chenDb := xorms.NewXormsDb(c.Mysql.Username, c.Mysql.Password, c.Mysql.Host, c.Mysql.Port, c.Mysql.Databases, c.Mysql.Showsql, c.Mysql.Migrate, c.Name)
	chenCache := redis.MustNewRedis(redis.RedisConf{
		Host: c.Cache.Host,
		Pass: c.Cache.Pass,
		Type: c.Cache.Type,
	})
	return &ServiceContext{
		Config:    c,
		ChenDb:    chenDb,
		ChenCache: chenCache,
		KqClient:  kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic),
		RpcMDLE:   rpcserver.NewMiddleware(),
		Es:        es.NewEs(c.EsConf.Host, c.EsConf.Port, c.EsConf.User, c.EsConf.Password),
	}
}
